package cn.gwm.flink.streaming.strategy.multidimension;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import cn.gwm.flink.streaming.strategy.vehiclepredict.DwsStrConstant;
import cn.gwm.utils.HBaseDataSource;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author : GW00280745
 * @date : 2022/12/26 10:38
 * @description :
 */
@Slf4j
public class PhoenixInfo implements Serializable {


    public JSONObject query(String condition) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        try {
            String sql = selectStr.concat(condition);
            System.out.println(sql);
//            log.info(sql)
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            while (rs.next()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(deviceIdKey,rs.getString(1));
                jsonObject.put(vinKey,rs.getString(1));
                jsonObject.put(itemTimeKey,rs.getString(2));
                jsonObject.put(tidKey,rs.getString(2));
                jsonObject.put(startChargeTimeKey,rs.getString(3));
                jsonObject.put(mapIdKey,rs.getString(4));
                jsonObject.put(currKey,rs.getString(5));
                jsonObject.put(innerSocKey,rs.getString(6));
                jsonObject.put(acAmbKey,rs.getString(7));
                jsonObject.put(modelCodeKey,rs.getString(8));
                jsonObject.put(endTimeKey,rs.getString(9));
                jsonObject.put(endSocKey,rs.getString(10));
                return jsonObject;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return null;
    }

    public List<JSONObject> queryMapId2(String condition, JSONObject line) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        ArrayList<JSONObject> list = new ArrayList<>();
        try {
            String sql = selectStr.concat(condition);
            if (StringUtils.startsWithIgnoreCase(condition, DwsStrConstant.ins().selectStr)){
                sql = condition;
            }
            System.out.println(sql);
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            HashSet<String> uniqSet = new HashSet<>();
            while (rs.next()) {
                String deviceId = rs.getString(1);
                //如果deviceid是空，
                if (StringUtils.isBlank(deviceId)){
                    continue;
                }
                String itemTime = rs.getString(2);
                String bmsInnersoc = rs.getString(3);
                String startchargetime = rs.getString(4);
                String uniqKey = deviceId.concat(startchargetime);
                //保留:一个deviceid 一次充电记录 保留一个
                if(uniqSet.contains(uniqKey)){
                    continue;
                }
                uniqSet.add(uniqKey);
                String bmsRmcModuletempmin = rs.getString(5);
                String e = rs.getString(6);
                if (e==null || StringUtils.isBlank(e)){continue;}
                String modelCode = rs.getString(7);
                String bmsBattcurrRm = rs.getString(8);
                String steadySoc = rs.getString(9);
                String steadyCurr = rs.getString(10);
                String steadyTempmin = rs.getString(11);
                String poiid = rs.getString(12);
                String aoiid = rs.getString(13);
                String steadyTime = rs.getString(14);
                if (steadyTime.length()!=13){
                    continue;
                }
                JSONObject json = new JSONObject();
                json.put(deviceIdKey,deviceId);
                json.put(itemTimeKey,itemTime);
                json.put(innerSocKey,bmsInnersoc);
                json.put(startChargeTimeKey,startchargetime);
                json.put(acAmbKey,bmsRmcModuletempmin);
                json.put("e",e);
                json.put(modelCodeKey,modelCode);
                json.put(currKey,bmsBattcurrRm);
                json.put(steadySocKey,steadySoc);
                json.put(steadyCurrKey,steadyCurr);
                json.put(steadyTempminKey,steadyTempmin);
                json.put(poiidKey,poiid);
                json.put(aoiidKey,aoiid);
                json.put(steadyTimeKey,steadyTime);
                list.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return list;
    }

    public List<JSONObject> query1Soc(String condition, JSONObject line) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        ArrayList<JSONObject> list = new ArrayList<>();
        try {
            String sql = selectStr1Soc.concat(condition);
            if (StringUtils.startsWithIgnoreCase(condition, DwsStrConstant.ins().selectStr)){
                sql = condition;
            }
            System.out.println(sql);
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            while (rs.next()) {
                String deviceId = rs.getString(1);
                //如果deviceid是空，
                if (StringUtils.isBlank(deviceId)){
                    continue;
                }
                String poiId = rs.getString(2);
                String soc1Avgtime = rs.getString(3);
                String modelCode = rs.getString(4);
                String currG = rs.getString(5);
                String modelSoc1Avgtime = rs.getString(6);
                String batterCapcity = rs.getString(7);
                JSONObject json = new JSONObject();
                json.put(deviceIdKey,deviceId);
                json.put(ChargePredictFields.ins().srcFieldPoiId,poiId);
                json.put(DwsStrConstant.ins().soc1Avgtime,soc1Avgtime);
                json.put(ChargePredictFields.ins().srcFieldModelCode,modelCode);
                json.put(DwsStrConstant.ins().currGroup,currG);
                json.put(DwsStrConstant.ins().modelSoc1Avgtime,modelSoc1Avgtime);
                json.put(DwsStrConstant.ins().batterCapcityStr,batterCapcity);
                list.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return list;
    }
    public List<JSONObject> query1SocV1(String condition, JSONObject line) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        ArrayList<JSONObject> list = new ArrayList<>();
        try {
            String sql = selectStr.concat(condition);
            if (StringUtils.startsWithIgnoreCase(condition, DwsStrConstant.ins().selectStr)){
                sql = condition;
            }
            System.out.println(sql);
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            HashSet<String> uniqSet = new HashSet<>();
            while (rs.next()) {
                String deviceId = rs.getString(1);
                //如果deviceid是空，
                if (StringUtils.isBlank(deviceId)){
                    continue;
                }
                String itemTime = rs.getString(2);
                String bmsInnersoc = rs.getString(3);
                String startchargetime = rs.getString(4);
                String uniqKey = deviceId.concat(startchargetime);
                //保留:一个deviceid 一次充电记录 保留一个
                if(uniqSet.contains(uniqKey)){
                    continue;
                }
                uniqSet.add(uniqKey);
                String bmsRmcModuletempmin = rs.getString(5);
                String e = rs.getString(6);
                if (e==null || StringUtils.isBlank(e)){continue;}
                String modelCode = rs.getString(7);
                String bmsBattcurrRm = rs.getString(8);
                String steadySoc = rs.getString(9);
                String steadyCurr = rs.getString(10);
                String steadyTempmin = rs.getString(11);
                String poiid = rs.getString(12);
                String aoiid = rs.getString(13);
                String steadyTime = rs.getString(14);
                if (steadyTime.length()!=13){
                    continue;
                }
                JSONObject json = new JSONObject();
                json.put(deviceIdKey,deviceId);
                json.put(itemTimeKey,itemTime);
                json.put(innerSocKey,bmsInnersoc);
                json.put(startChargeTimeKey,startchargetime);
                json.put(acAmbKey,bmsRmcModuletempmin);
                json.put("e",e);
                json.put(modelCodeKey,modelCode);
                json.put(currKey,bmsBattcurrRm);
                json.put(steadySocKey,steadySoc);
                json.put(steadyCurrKey,steadyCurr);
                json.put(steadyTempminKey,steadyTempmin);
                json.put(poiidKey,poiid);
                json.put(aoiidKey,aoiid);
                json.put(steadyTimeKey,steadyTime);
                list.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return list;
    }
    public List<JSONObject> query1SocV2(String condition, JSONObject line) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        ArrayList<JSONObject> list = new ArrayList<>();
        try {
            String sql = selectStr.concat(condition);
            if (StringUtils.startsWithIgnoreCase(condition, DwsStrConstant.ins().selectStr)){
                sql = condition;
            }
            System.out.println(sql);
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            HashSet<String> uniqSet = new HashSet<>();
            while (rs.next()) {
                String deviceId = rs.getString(1);
                //如果deviceid是空，
                if (StringUtils.isBlank(deviceId)){
                    continue;
                }
                String itemTime = rs.getString(2);
                String bmsInnersoc = rs.getString(3);
                String startchargetime = rs.getString(4);
                String uniqKey = deviceId.concat(startchargetime);
                //保留:一个deviceid 一次充电记录 保留一个
                if(uniqSet.contains(uniqKey)){
                    continue;
                }
                uniqSet.add(uniqKey);
                String bmsRmcModuletempmin = rs.getString(5);
                String e = rs.getString(6);
                if (e==null || StringUtils.isBlank(e)){continue;}
                String modelCode = rs.getString(7);
                String bmsBattcurrRm = rs.getString(8);
                String steadySoc = rs.getString(9);
                String steadyCurr = rs.getString(10);
                String steadyTempmin = rs.getString(11);
                String poiid = rs.getString(12);
                String aoiid = rs.getString(13);
                String steadyTime = rs.getString(14);
                if (steadyTime.length()!=13){
                    continue;
                }
                JSONObject json = new JSONObject();
                json.put(deviceIdKey,deviceId);
                json.put(itemTimeKey,itemTime);
                json.put(innerSocKey,bmsInnersoc);
                json.put(startChargeTimeKey,startchargetime);
                json.put(acAmbKey,bmsRmcModuletempmin);
                json.put("e",e);
                json.put(modelCodeKey,modelCode);
                json.put(currKey,bmsBattcurrRm);
                json.put(steadySocKey,steadySoc);
                json.put(steadyCurrKey,steadyCurr);
                json.put(steadyTempminKey,steadyTempmin);
                json.put(poiidKey,poiid);
                json.put(aoiidKey,aoiid);
                json.put(steadyTimeKey,steadyTime);
                list.add(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return list;
    }
    public JSONObject queryMapId(String condition,JSONObject line) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        JSONObject res = null;
        try {
            Object mapid= line.get(mapIdKey);
            boolean b = mapid != null
                    && StringUtils.isNotBlank(mapid.toString())
                    && !StringUtils.equalsIgnoreCase(mapid.toString(), defaultStr);
            String sql = selectStr.concat(condition);
            System.out.println(sql);
//            log.info(sql)
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            JSONObject firstJson = null;
            int cnt = 0 ;
            while (rs.next()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(deviceIdKey,rs.getString(1));
                jsonObject.put(vinKey,rs.getString(1));
                jsonObject.put(itemTimeKey,rs.getString(2));
                jsonObject.put(tidKey,rs.getString(2));
                jsonObject.put(startChargeTimeKey,rs.getString(3));
                String mapIdVal = rs.getString(4);
                jsonObject.put(mapIdKey,mapIdVal);
                jsonObject.put(currKey,rs.getString(5));
                jsonObject.put(innerSocKey,rs.getString(6));
                jsonObject.put(acAmbKey,rs.getString(7));
                jsonObject.put(modelCodeKey,rs.getString(8));
                jsonObject.put(endTimeKey,rs.getString(9));
                jsonObject.put(endSocKey,rs.getString(10));
                jsonObject.put(steadyTimeKey,rs.getString(11));
                jsonObject.put(steadySocKey,rs.getString(12));
                jsonObject.put(steadyCurrKey,rs.getString(13));
                jsonObject.put(steadyTempminKey,rs.getString(14));
                if (b){
                    //如果地点值是非空，那么找到第一个相等的记录
                    if (StringUtils.equalsIgnoreCase(mapid.toString(),mapIdVal)){
                        return jsonObject;
                    }
                }else {
                    //如果地点值是空，那么找到第一个记录
                    return jsonObject;
                }
                if (cnt==0){
                    firstJson = jsonObject;
                }
                cnt++;
            }
            if (b){return firstJson;}
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return null;
    }

    DimensionOrder dimension = new DimensionOrder();
//    private String selectStr = "select substr(id,0,17) as \"deviceid\",reverse(substr(id,19,32)) as \"item_time\",\"bms_innersoc\",\"startchargetime\",\"bms_rmc_moduletempmin\",\"e\",\"model_code\",\"bms_battcurr_rm\",\"steady_soc\",\"steady_curr\",\"steady_tempmin\",\"poiid\",\"aoiid\",\"steady_time\" from \"vaas_dws\".\"dws_multidimension_charge_obc_v2\" "
    private String selectStr = "select substr(id,0,17) as \"deviceid\",reverse(substr(id,19,32)) as \"item_time\",\"bms_innersoc\",\"startchargetime\",\"bms_rmc_moduletempmin\",\"e\",\"model_code\",\"bms_battcurr_rm\",\"steady_soc\",\"steady_curr\",\"steady_tempmin\",\"poiid\",\"aoiid\",\"steady_time\" from \"vaas_dws\".\"dws_multidimension_charge_obc_v3\" ";

    private String selectStr1Soc = "select substr(id,0,17) as \"deviceid\",\"poiid\",\"soc1_avgtime\",\"model_code\",\"curr_g\",\"model_soc1_avgtime\",\"batter_capcity\" from \"vaas_dws\".\"multidimension_obc_soc1_1\" ";

    private String selectStrV1 = "select \"deviceid\",\"item_time\",\"startchargetime\",round((to_number(\"endchargetime\") - to_number(\"item_time\"))/(60*1000),1) as d,round((to_number(\"end_bms_soc\") - to_number(\"bms_innersoc\")),1) as c,round(( round((to_number(\"endchargetime\") - to_number(\"item_time\"))/(60.0*1000),1) ) / ( round((to_number(\"end_bms_soc\") - to_number(\"bms_innersoc\")),1) ),1) as e,\"mapid\",\"bms_battcurr_rm\",\"bms_innersoc\",\"bms_rmc_moduletempmin\",\"model_code\",\"endchargetime\",\"end_bms_soc\",\"steady_time\",\"steady_soc\",\"steady_curr\",\"steady_tempmin\" from \"vaas_dws\".\"dws_multidimension_charge\" ";
    private String innerSocKey = "bms_innersoc";
    private String modelCodeKey = "model_code";
    private String startChargeTimeKey = "startChargeTime";
    private String vinKey = "vin";
    private String deviceIdKey = "deviceId";
    private String tidKey = "tid";
    private String itemTimeKey = "item_time";
    private String mapIdKey = "mapId";
    private String currKey = "bms_batt_curr_rm";
    private String inletTempKey = "bms_inlettemps";
    private String heatReqKey = "bms_heatreq";
    private String acOpenKey = "acopensts";
    private String socLimKey = "tbox_batt_soc_lim";
    private String acAmbKey = "bms_rmc_moduletempmin";
    private String acAmbKey2 = "bms_rmc_module_temp_min";
    private String sohKey = "soh_cor";
    private String endTimeKey = "endchargetime";
    private String endSocKey = "end_bms_soc";
    private String steadyTimeKey = "steady_time";
    private String steadySocKey = "steady_soc";
    private String steadyCurrKey = "steady_curr";
    private String steadyTempminKey = "steady_tempmin";
    private String poiidKey = "poiid";
    private String aoiidKey = "aoiid";
    private String whereStr = " where ";

    public String startStageQuery(JSONObject line){
        String vin= line.getString(vinKey);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        Object mapId= line.get(mapIdKey);
        Object bmsBattcurr= line.get(currKey);
        Object acambtemp= line.get(acAmbKey);
        if (acambtemp==null){
            acambtemp = line.get(acAmbKey2);
        }
        //车辆vin、时间、地点、电流、温度
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        dimension.deviceIdDim(stringBuilder,vin,true);
        dimension.andOps(stringBuilder);
        boolean firstTime = line.getBooleanValue(ChargePredictFields.ins().firstTimeField);
        dimension.startTimeDim(stringBuilder,startChargeTime,firstTime);
        dimension.andOps(stringBuilder);
        dimension.currDim(stringBuilder,bmsBattcurr,false,10);
        dimension.andOps(stringBuilder);
        dimension.tempDim(stringBuilder,acambtemp,false);
//        dimension.andOps(stringBuilder)
//        dimension.startStageSoc(stringBuilder)
        dimension.orderBy1(stringBuilder,acambtemp,bmsBattcurr);
        return stringBuilder.toString();
    }
    public String startQuery(JSONObject line, Float bmsSoc,Object steadyCurr,Object steadyTempmin){
        Object vin= line.get(vinKey);
        Long tidVal = line.getLong(tidKey);
        Object mapId= line.get(mapIdKey);
        Object bmsBattcurr= line.get(currKey);
        Object bmsInletTemps= line.get(inletTempKey);
        Object bmsHeatreq= line.get(heatReqKey);
        Object acopenSts= line.get(acOpenKey);
        Object tboxBattSocLim= line.get(socLimKey);
        Object acambTemp= line.get(acAmbKey);
        if (steadyTempmin != null ){
            acambTemp = steadyTempmin;
        }
        if (acambTemp==null){
            acambTemp = line.get(acAmbKey2);
        }
        Object soh= line.get(sohKey);
        String poiId = line.getString(ChargePredictFields.ins().srcFieldPoiId);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        boolean firstTime = line.getBooleanValue(ChargePredictFields.ins().firstTimeField);
        //车辆vin、时间、地点、电流、起点soc、温度、目标soc
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        dimension.deviceIdDim2(stringBuilder,vin,true);
        dimension.andOps(stringBuilder);

        dimension.startTimeDim(stringBuilder,startChargeTime,firstTime);
        dimension.andOps(stringBuilder);
        dimension.socDim3(stringBuilder,bmsSoc,2);
        dimension.andOps(stringBuilder);
        dimension.tempDim2(stringBuilder,acambTemp,false);
        dimension.andOps(stringBuilder);
        dimension.currDimObc(stringBuilder,bmsBattcurr,true,2);
        boolean b = StringUtils.isNotBlank(poiId) && !StringUtils.equalsIgnoreCase(poiId, ChargePredictFields.ins().blankStr9);
        if(b){
//            dimension.andOps(stringBuilder)
//            dimension.poiidDim(stringBuilder,poiId)
        }

        dimension.orderByObc2(stringBuilder,null,null);
        //拼接
        return stringBuilder.toString();
//        return format;
    }
    public String startQuery1Soc(JSONObject line){
        Object vin= line.get(vinKey);
        Object poiId= line.get(ChargePredictFields.ins().srcFieldPoiId);
        Object bmsBattcurr= line.get(currKey);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        //车辆vin、时间、地点、电流、起点soc、温度、目标soc
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        dimension.deviceIdDim2(stringBuilder,vin,true);

        dimension.andOps(stringBuilder);
        dimension.currgDim(stringBuilder,bmsBattcurr.toString());

        boolean b = poiId == null || StringUtils.isBlank(poiId.toString()) || StringUtils.equalsIgnoreCase(poiId.toString(), ChargePredictFields.ins().blankStr9);
        if (!b){
//            dimension.andOps(stringBuilder)
//            dimension.poiidDim(stringBuilder,poiId)
        }
        //拼接
        return stringBuilder.toString();
//        return format;
    }
    public String startQuery1SocV1(JSONObject line){
        Object vin= line.get(vinKey);
        Long tidVal = line.getLong(tidKey);
        Object mapId= line.get(mapIdKey);
        Object bmsBattcurr= line.get(currKey);
        Object bmsInletTemps= line.get(inletTempKey);
        Object bmsHeatreq= line.get(heatReqKey);
        Object acopenSts= line.get(acOpenKey);
        Object tboxBattSocLim= line.get(socLimKey);
        Object acambTemp= line.get(acAmbKey);
        if (acambTemp==null){
            acambTemp = line.get(acAmbKey2);
        }
        Object soh= line.get(sohKey);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        //车辆vin、时间、地点、电流、起点soc、温度、目标soc
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        dimension.deviceIdDim2(stringBuilder,vin,true);
        dimension.andOps(stringBuilder);
        boolean firstTime = line.getBooleanValue(ChargePredictFields.ins().firstTimeField);
        dimension.startTimeDim(stringBuilder,startChargeTime,firstTime);
//        dimension.andOps(stringBuilder)
//        dimension.socDim3(stringBuilder,bmsSoc,2)
//        dimension.andOps(stringBuilder)
//        dimension.tempDim2(stringBuilder,acambTemp,false)
        dimension.andOps(stringBuilder);
        dimension.currDimObc(stringBuilder,bmsBattcurr,true,2);

        dimension.orderByObc2(stringBuilder,null,null);
        //拼接
        return stringBuilder.toString();
    }
    public String startQuery1SocV2(JSONObject line){
        Object vin= line.get(vinKey);
        Long tidVal = line.getLong(tidKey);
        Object mapId= line.get(mapIdKey);
        Object bmsBattcurr= line.get(currKey);
        Object bmsInletTemps= line.get(inletTempKey);
        Object bmsHeatreq= line.get(heatReqKey);
        Object acopenSts= line.get(acOpenKey);
        Object tboxBattSocLim= line.get(socLimKey);
        Object acambTemp= line.get(acAmbKey);
        if (acambTemp==null){
            acambTemp = line.get(acAmbKey2);
        }
        Object soh= line.get(sohKey);
        Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
        String modelCode = line.getString(ChargePredictFields.ins().srcFieldModelCode);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        //车辆vin、时间、地点、电流、起点soc、温度、目标soc
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        boolean bM = modelCode == null || StringUtils.isBlank(modelCode) || StringUtils.equalsIgnoreCase(modelCode, ChargePredictFields.ins().blankStr9);
        if (!bM){
            dimension.modelDim(stringBuilder,modelCode);
            dimension.andOps(stringBuilder);
        }
        boolean firstTime = line.getBooleanValue(ChargePredictFields.ins().firstTimeField);
        dimension.startTimeDim(stringBuilder,startChargeTime,firstTime);
        dimension.andOps(stringBuilder);
        dimension.socDim3(stringBuilder,bmsSoc,2);
        dimension.andOps(stringBuilder);
        dimension.tempDim2(stringBuilder,acambTemp,false);
        dimension.andOps(stringBuilder);
        dimension.currDimObc1SocV2(stringBuilder,bmsBattcurr,2);

        dimension.orderByObc3(stringBuilder,null,null);
        //拼接
        return stringBuilder.toString();
    }
    public String startQuery1SocModel(JSONObject line){
        Object vin= line.get(vinKey);
        Object poiId= line.get(ChargePredictFields.ins().srcFieldPoiId);
        Object bmsBattcurr= line.get(currKey);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        String modelCode = line.getString(ChargePredictFields.ins().srcFieldModelCode);
        String batterCapacity = line.getString(ChargePredictFields.ins().srcFieldBatterCapacity);
        //车辆vin、时间、地点、电流、起点soc、温度、目标soc
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        boolean bM = modelCode == null || StringUtils.isBlank(modelCode) || StringUtils.equalsIgnoreCase(modelCode, ChargePredictFields.ins().blankStr9);
        if (!bM){
            dimension.modelDim(stringBuilder,modelCode);
            dimension.andOps(stringBuilder);
        }
        boolean bB = batterCapacity == null || StringUtils.isBlank(batterCapacity) || StringUtils.equalsIgnoreCase(batterCapacity, ChargePredictFields.ins().blankStr9);
        if (!bB){
//            dimension.batterCapDim(stringBuilder,batterCapacity)
//            dimension.andOps(stringBuilder)
        }
        dimension.currgDim(stringBuilder,bmsBattcurr.toString());
        stringBuilder.append(" limit 100 ");
        //拼接
        return stringBuilder.toString();
//        return format;
    }
    public String startObcCurr(JSONObject line, Float bmsSoc, boolean endSocFlag,boolean isObc,int currStepVal,boolean mapIdBool,boolean breakBool){
        Object vin= line.get(vinKey);
        Long tidVal = line.getLong(tidKey);
        Object mapId= line.get(mapIdKey);
        Object bmsBattcurr= line.get(currKey);
        Object bmsInlettemps= line.get(inletTempKey);
        Object bmsHeatreq= line.get(heatReqKey);
        Object acopensts= line.get(acOpenKey);
        Object tboxBattSocLim= line.get(socLimKey);
        Object acambtemp= line.get(acAmbKey);
        if (acambtemp==null){
            acambtemp = line.get(acAmbKey2);
        }
        Object soh= line.get(sohKey);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        //车辆vin、地点、电流、起点soc、电池温度、目标soc、加热、空调开启
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
//        if (
//                mapid  null
//                        && StringUtils.isNotBlank(mapid.toString())
//                        && !StringUtils.equalsIgnoreCase(mapid.toString(),defaultStr)
//        ){
//            if (mapIdBool){
//                dimension.mapIdDim(stringBuilder,mapid.toString())
//                dimension.andOps(stringBuilder)
//            }
//        }
        dimension.deviceIdDim(stringBuilder,vin,true);
        dimension.andOps(stringBuilder);
        boolean firstTime = line.getBooleanValue(ChargePredictFields.ins().firstTimeField);
        dimension.startTimeDim(stringBuilder,startChargeTime,firstTime);
        dimension.andOps(stringBuilder);
        dimension.currDim(stringBuilder,bmsBattcurr,isObc,currStepVal);
        dimension.andOps(stringBuilder);
        dimension.socDim(stringBuilder,bmsSoc,endSocFlag);
        dimension.andOps(stringBuilder);
        dimension.tempDim(stringBuilder,acambtemp,breakBool);
        if (breakBool){
            dimension.andOps(stringBuilder);
            dimension.endSocDim(stringBuilder);
        }
        dimension.orderBy(stringBuilder,acambtemp,bmsBattcurr,bmsSoc);
        //拼接
        return stringBuilder.toString();
    }
    private String whereQuotation = "\'";
    private String whereAnd = " and ";
    private String whereDeviceId = " \"deviceid\"=\'";
    private String whereStartChargeTime = " \"startchargetime\"=\'";
    private String whereTimeOps = " and \"item_time\">=\"startchargetime\" ";
    private String defaultStr = "-999999";


    public String startObcTemp(JSONObject line, Float bmsSoc, boolean endSocFlag,boolean isObc,int currStepVal,boolean mapIdBool,boolean breakBool){
        Object vin= line.get(vinKey);
        Long tidVal = line.getLong(tidKey);
        Object mapid= line.get(mapIdKey);
        Object bmsBattcurr= line.get(currKey);
        Object bmsInlettemps= line.get(inletTempKey);
        Object bmsHeatreq= line.get(heatReqKey);
        Object acopensts= line.get(acOpenKey);
        Object tboxBattSocLim= line.get(socLimKey);
        Object acambtemp= line.get(acAmbKey);
        if (acambtemp==null){
            acambtemp=line.get(acAmbKey2);
        }
        Object soh= line.get(sohKey);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        //车辆vin、地点、电流、起点soc、电池温度、目标soc、加热、空调开启
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
//        if (
//                mapid  null
//                        && StringUtils.isNotBlank(mapid.toString())
//                        && !StringUtils.equalsIgnoreCase(mapid.toString(),defaultStr)
//        ){
//            if (mapIdBool){
//                dimension.mapIdDim(stringBuilder,mapid.toString())
//                dimension.andOps(stringBuilder)
//            }
//        }
        dimension.deviceIdDim(stringBuilder,vin,true);
        dimension.andOps(stringBuilder);
        boolean firstTime = line.getBooleanValue(ChargePredictFields.ins().firstTimeField);
        dimension.startTimeDim(stringBuilder,startChargeTime,firstTime);
        dimension.andOps(stringBuilder);
        dimension.currDim(stringBuilder,bmsBattcurr,isObc,currStepVal);
        dimension.andOps(stringBuilder);
        dimension.socDim(stringBuilder,bmsSoc,endSocFlag);
//        dimension.andOps(stringBuilder)
//        dimension.tempDim(stringBuilder,acambtemp,breakBool)
        if (breakBool){
            dimension.andOps(stringBuilder);
            dimension.endSocDim(stringBuilder);
        }
        dimension.orderBy(stringBuilder,acambtemp,bmsBattcurr,bmsSoc);
        //拼接
        return stringBuilder.toString();
    }
    public String endSoc(JSONObject line){
        Object vin= line.get(vinKey);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        stringBuilder.append(whereDeviceId).append(vin).append(whereQuotation);
        long startChargeTime = line.getLongValue(startChargeTimeKey);
        stringBuilder.append(whereAnd).append(whereStartChargeTime).append(startChargeTime).append(whereQuotation);
        stringBuilder.append(whereTimeOps);
        dimension.orderByEndSoc(stringBuilder);
        return stringBuilder.toString();
    }
    public String startQueryV1(JSONObject line, Float bmsSoc,Object steadyCurr,Object steadyTempmin){
        Object vin= line.get(vinKey);
        Long tidVal = line.getLong(tidKey);
        Object mapid= line.get(mapIdKey);
        Object bmsBattcurr= line.get(currKey);
        Object bmsInlettemps= line.get(inletTempKey);
        Object bmsHeatreq= line.get(heatReqKey);
        Object acopensts= line.get(acOpenKey);
        Object tboxBattSocLim= line.get(socLimKey);
        Object acambtemp= line.get(acAmbKey);
        if (acambtemp==null){
            acambtemp = line.get(acAmbKey2);
        }
        Object soh= line.get(sohKey);
        String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
        //车辆vin、时间、地点、电流、起点soc、温度、目标soc
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(whereStr);
        dimension.chrgNumDim(stringBuilder);
        dimension.andOps(stringBuilder);
        dimension.deviceIdDim(stringBuilder,vin,true);
        dimension.andOps(stringBuilder);
        boolean firstTime = line.getBooleanValue(ChargePredictFields.ins().firstTimeField);
        dimension.startTimeDim(stringBuilder,startChargeTime,firstTime);
        dimension.andOps(stringBuilder);
        dimension.currDim(stringBuilder,bmsBattcurr,false,10);
        dimension.andOps(stringBuilder);
        dimension.socDim2(stringBuilder,bmsSoc,5);
        dimension.andOps(stringBuilder);
        dimension.tempDim(stringBuilder,acambtemp,false);
        dimension.andOps(stringBuilder);
        dimension.testS(stringBuilder);
        dimension.orderBy3(stringBuilder,acambtemp,bmsBattcurr);
        //拼接
        return stringBuilder.toString();
//        return format;
    }
    public Float queryMapId3(String condition,JSONObject line,float innerSoc) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        JSONObject res = null;
        try {
            Object mapid= line.get(mapIdKey);
            boolean b = mapid != null
                    && StringUtils.isNotBlank(mapid.toString())
                    && !StringUtils.equalsIgnoreCase(mapid.toString(), defaultStr);
            String sql = selectStrV1.concat(condition);
            if (StringUtils.startsWithIgnoreCase(condition,DwsStrConstant.ins().selectStr)){
                sql = condition;
            }
            System.out.println(sql);
//            log.info(sql)
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            Float curr = line.getFloat(currKey);
            Float temp = line.getFloat(acAmbKey2);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            JSONObject firstJson = new JSONObject();
            int cnt = 0 ;
            float total = 0 ;

            while (rs.next()) {
                float timeVal = rs.getFloat(4);
                total += timeVal;
                cnt++;
            }
            if (cnt==0){
                return null;
            }
            float v = total / cnt;
            return v;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return null;
    }
    public Float queryMapId4(String condition,JSONObject line,float innerSoc) {
        //查一个曲线的开始点 一条曲线只需要查找开始点，之后根据开始点查找结束点，这样就是一条曲线
        Connection connection = null;
        JSONObject res = null;
        try {
            Object mapid= line.get(mapIdKey);
            boolean b = mapid != null
                    && StringUtils.isNotBlank(mapid.toString())
                    && !StringUtils.equalsIgnoreCase(mapid.toString(), defaultStr);
            String sql = selectStrV1.concat(condition);
            if (StringUtils.startsWithIgnoreCase(condition,DwsStrConstant.ins().selectStr)){
                sql = condition;
            }
            System.out.println(sql);
//            log.info(sql)
            connection = HBaseDataSource.ins().getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            Float curr = line.getFloat(currKey);
            Float temp = line.getFloat(acAmbKey2);
            ResultSet rs = null;
            try {
                rs = preparedStatement.executeQuery();
            }catch (Exception e){
                //第一次如果报错了，那么就重试一次，如果还是错误先跳过
                rs = preparedStatement.executeQuery();
            }
            JSONObject firstJson = new JSONObject();
            int cnt = 0 ;
            float total = 0 ;

            while (rs.next()) {
                float timeVal = rs.getFloat(4);
//                total += timeVal
                float deltaSoc = rs.getFloat(5);
                if (deltaSoc<=0){continue;}
                float avgTime = rs.getFloat(6);
                total += avgTime;
                cnt++;
            }
            if (cnt==0){
                return null;
            }
            float v = total / cnt;
            return v;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            HBaseDataSource.ins().close(connection);
        }
        return null;
    }
}
